package com.zp.business.impl.outpFees;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.outpFees.OutpFeesDetail;
import com.zp.business.entity.outpFees.bo.OutpFeesDetailBo;
import com.zp.business.entity.outpFees.vo.OutpFeesDetailVo;
import com.zp.business.mapper.outpFees.OutpFeesDetailMapper;
import com.zp.business.service.outpFees.IOutpFeesDetailService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 门诊收费记录明细Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class OutpFeesDetailServiceImpl implements IOutpFeesDetailService {

    private final OutpFeesDetailMapper baseMapper;

    /**
     * 查询门诊收费记录明细
     */
    @Override
    public OutpFeesDetailVo queryById (String outpFeesDetailId) {
        return baseMapper.selectVoById(outpFeesDetailId);
    }

    /**
     * 查询门诊收费记录明细列表(分页)
     */
    @Override
    public TableDataInfo<OutpFeesDetailVo> queryPageList (OutpFeesDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OutpFeesDetail> lqw = buildQueryWrapper(bo);
        Page<OutpFeesDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门诊收费记录明细列表(不分页)
     */
    @Override
    public List<OutpFeesDetailVo> queryList (OutpFeesDetailBo bo) {
        LambdaQueryWrapper<OutpFeesDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OutpFeesDetail> buildQueryWrapper (OutpFeesDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OutpFeesDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOutpFeesMasterId()), OutpFeesDetail::getOutpFeesMasterId, bo.getOutpFeesMasterId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeesNo()), OutpFeesDetail::getFeesNo, bo.getFeesNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeFlag()), OutpFeesDetail::getChargeFlag, bo.getChargeFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getSerialNo()), OutpFeesDetail::getSerialNo, bo.getSerialNo());
        lqw.eq(StringUtils.isNotBlank(bo.getItemClass()), OutpFeesDetail::getItemClass, bo.getItemClass());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), OutpFeesDetail::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), OutpFeesDetail::getItemCode, bo.getItemCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), OutpFeesDetail::getRemarks, bo.getRemarks());
        lqw.orderByAsc(OutpFeesDetail::getFeesNo).orderByAsc(OutpFeesDetail::getItemClass)
                .orderByAsc(OutpFeesDetail::getApplyNo).orderByAsc(OutpFeesDetail::getPackItemName);
        return lqw;
    }

    /**
     * 新增门诊收费记录明细
     */
    @Override
    public Boolean insertByBo (OutpFeesDetailBo bo) {
        OutpFeesDetail add = BeanUtil.toBean(bo, OutpFeesDetail.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOutpFeesDetailId(add.getOutpFeesDetailId());
        }
        return flag;
    }

    /**
     * 修改门诊收费记录明细
     */
    @Override
    public Boolean updateByBo (OutpFeesDetailBo bo) {
        OutpFeesDetail update = BeanUtil.toBean(bo, OutpFeesDetail.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (OutpFeesDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门诊收费记录明细
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 修改收费状态
     *
     * @param chargeType            收费状态
     * @param clinicOrderIdOrCostId 门诊医嘱id或者costId
     * @return
     */
    @Override
    public Boolean updateChargeType (String chargeType, String clinicOrderIdOrCostId, String type) {
        return baseMapper.update(null, new LambdaUpdateWrapper<OutpFeesDetail>()
                .set(OutpFeesDetail::getChargeFlag, chargeType)
                .eq(StringUtils.equals(type, BaseConstants.ITEM_REFUND_OR_CANCEL.ALL), OutpFeesDetail::getClinicOrderId, clinicOrderIdOrCostId)
                .eq(StringUtils.equals(type, BaseConstants.ITEM_REFUND_OR_CANCEL.SINGLE), OutpFeesDetail::getApplyItemId, clinicOrderIdOrCostId)
        ) > 0;
    }
}
